package com.dc.project.system.controller;

import com.dc.common.validated.InsertGroup;
import com.dc.common.validated.QueryPageGroup;
import com.dc.common.validated.UpdateGroup;
import com.dc.common.vo.ResultVo;
import com.dc.project.system.dto.SysPostDto;
import com.dc.project.system.dto.SysUserPostDto;
import com.dc.project.system.entity.SysPost;
import com.dc.project.system.service.SysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * 系统用户岗位表(SysPost)表控制层
 *
 * @author zhuangcy
 * @since 2021-06-08 12:37:12
 */
@RestController
@RequestMapping("sysPost")
@Api(tags = "系统用户岗位表")
public class SysPostController {
    /**
     * 服务对象
     */
    @Autowired
    private SysPostService sysPostService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("通过主键查询单条数据")
    @RequiresPermissions("system:sysPost:query")
    public ResultVo get(@PathVariable Serializable id) {
        return this.sysPostService.queryById(id);
    }

    /**
     * 分页查询系统用户岗位表
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("分页查询系统用户岗位表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
    @RequiresPermissions("system:sysPost:query")
    public ResultVo listPage(@Validated(QueryPageGroup.class) SysPostDto dto) {
        return this.sysPostService.queryByPage(dto);
    }

    /**
     * 新增系统用户岗位表
     *
     * @param sysPost
     * @return
     */
    @PostMapping
    @ApiOperation("新增系统用户岗位表")
    @RequiresPermissions("system:sysPost:add")
    public ResultVo add(@Validated(value = {InsertGroup.class}) @RequestBody SysPost sysPost) {
        return this.sysPostService.add(sysPost);
    }

    /**
     * 编辑系统用户岗位表
     *
     * @param sysPost
     * @return
     */
    @PutMapping
    @ApiOperation("编辑系统用户岗位表")
    @RequiresPermissions("system:sysPost:edit")
    public ResultVo edit(@Validated(value = {UpdateGroup.class}) @RequestBody SysPost sysPost) {
        return this.sysPostService.edit(sysPost);
    }

    /**
     * 根据主键删除系统用户岗位表
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据主键删除系统用户岗位表")
    @RequiresPermissions("system:sysPost:delete")
    public ResultVo delete(@PathVariable Integer id) {
        return this.sysPostService.delete(id);
    }


    /**
     * 获取用户岗位
     *
     * @param dto
     * @return
     */
    @GetMapping("userPost")
    @ApiOperation("获取用户岗位")
    @RequiresPermissions(value = {"system:sysPost:query", "system:sysUser:add", "system:sysUser:edit"}, logical = Logical.OR)
    public ResultVo getUserPost(SysUserPostDto dto) {
        return this.sysPostService.getUserPost(dto);
    }

    /**
     * 根据用户id获取用户岗位
     *
     * @param userId
     * @return
     */
    @GetMapping("userPost/{userId}")
    @ApiOperation("根据用户id获取用户岗位")
    @RequiresPermissions("system:sysPost:query")
    public ResultVo getUserPostByUserId(@PathVariable Integer userId) {
        return this.sysPostService.getUserPostByUserId(userId);
    }

}